﻿using System;
using System.Collections.Generic;
using System.Text;
using RS.DOM.Common;
using RS.DOM.Construction;

namespace RS.DOM.Types
{
    public class RationalValue
    {
        public int Numerator;
        public int Denominator;

        public override string ToString()
        {
            return Numerator.ToString() + "/" + Denominator.ToString();
        }
    }

    public class Rational: Token
    {
        #region Constructors

        public Rational(Context context)
            : base(context)
        {
            base.TokenType = TokenType.Rational;
            this.Result = new RationalValue();
        }

        public Rational(Context context, RationalValue result)
            :base(context, result)
        {
            base.TokenType = TokenType.Rational;
            this.Result = new RationalValue();
        }

        #endregion

        #region Methods

        public override Token Operation(Token token, OperationType type)
        {
            Rational result = this;

            if(token.TokenType == TokenType.Rational)
            {
                Rational op1 = (Rational)this;
                Rational op2 = (Rational)token;

                result = new Rational(this.Context);
                if (type == OperationType.Plus)
                {
                    result.Result.Numerator = op1.Result.Numerator * op2.Result.Denominator + op2.Result.Numerator * op1.Result.Denominator;
                    result.Result.Denominator = op1.Result.Denominator * op2.Result.Denominator;
                }
            }
            else
            {
                base.Operation(token, type);
            }

            return result;
        }

        public RationalValue Result
        {
            get
            {
                return (RationalValue)base.Result;
            }

            set
            {
                base.Result = value;
            }
        }

        #endregion
    }
}
